﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PessMvc.Models;
using PessModel;
using System.Data;

namespace PessMvc.Lib
{
    public class ArchiveBL:IArchiveBL
    {
        private PESSEntities db;
        private ModelStateDictionary _errorLog;
        public ArchiveBL(ModelStateDictionary modelState)
        {
            db = new PESSEntities();
            _errorLog = modelState;
        }
        public void AddFeedback(FeedbackModel feedback,int subjectID)
        {
            if (this.IfFeedbackEmpty(feedback.ArchiveSerialNumber))
            {
                var archive = db.Archives.Where(a => a.SerialNumber == feedback.ArchiveSerialNumber).FirstOrDefault();
                var subject=db.Subjects.Find(subjectID);
                if (archive == null)
                {
                    _errorLog.AddModelError("档案不存在", "档案不存在，请确认档案号。");
                }
                else if (archive.SubjectID != subjectID)
                {
                    _errorLog.AddModelError("档案非本学科组", string.Format("档案({0})不属于学科({1})", archive.SerialNumber, subject.Name));
                }
                else
                {
                    archive.ProjectSN = feedback.ProjectSN;
                    archive.ReviewFeedback = feedback.ReviewFeedback;
                    archive.Conclusion = feedback.Conclusion;
                    db.Entry(archive).State = EntityState.Modified;
                    db.SaveChanges();
                }
            }
            else
            {
                _errorLog.AddModelError("反馈意见已经存在", string.Format("档案({0})的反馈意见已经存在，不可覆盖。",feedback.ArchiveSerialNumber));
            }

        }

        private bool IfFeedbackEmpty(string archiveSerialNumber)
        {
            bool ifNoFeedback = true;
            var archive = db.Archives.Where(a => a.SerialNumber.Trim() == archiveSerialNumber.Trim()).FirstOrDefault();
            if (archive != null 
                && ((archive.ReviewFeedback!=null&& archive.ReviewFeedback.Trim() != "")
                || (archive.Conclusion!=null&&archive.Conclusion.Trim() != "")
                || (archive.ProjectSN!=null&&archive.ProjectSN.Trim() != "")))
            {
                ifNoFeedback = false;
            }
            return ifNoFeedback;
        }

        public IEnumerable<FeedbackModel> GetFeedbacks(int subjectID)
        {
            var feedbacks=from a in db.Archives.Where(a => a.SubjectID == subjectID)
                          select new FeedbackModel() { ArchiveSerialNumber = a.SerialNumber, Conclusion = a.Conclusion, ReviewFeedback = a.ReviewFeedback, ProjectSN = a.ProjectSN };
            return feedbacks;
        }

        public FeedbackModel ArchiveFeedback(string serialNumber)
        {
            FeedbackModel model=null;
            var archive= db.Archives.Where(a => a.SerialNumber == serialNumber).FirstOrDefault();
            if (archive != null)
            {
                model = new FeedbackModel() { ArchiveSerialNumber = serialNumber, Conclusion = archive.Conclusion, ProjectSN = archive.ProjectSN, ReviewFeedback = archive.ReviewFeedback };
            }
            else
            {
                _errorLog.AddModelError("档案不存在", string.Format("档案({0})不存在。", serialNumber));
            }
            return model;
        }

        public bool UpdateFeedback(FeedbackModel model)
        {
            bool updateSuccess=false;
            var archive = db.Archives.Where(a => a.SerialNumber == model.ArchiveSerialNumber).FirstOrDefault();
            if (archive != null)
            {
                archive.ProjectSN = model.ProjectSN;
                archive.Conclusion = model.Conclusion;
                archive.ReviewFeedback = model.ReviewFeedback;
                db.Entry(archive).State = EntityState.Modified;
                db.SaveChanges();

                updateSuccess=true;
            }
            else
            {
                _errorLog.AddModelError("档案不存在", string.Format("档案({0})不存在。", model.ArchiveSerialNumber));
                updateSuccess=false;
            }
            return updateSuccess;
        }
    }
}